package com.jy.wms.common.base;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.UserInfoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:zzp
 * @Description: Created by Administrator on 2017/8/13.
 * @Date:Create in 21:40 2017/8/13
 */
public abstract class BaseService<M extends BaseDao<T>,T extends BaseModel> extends ServiceImpl<M,T> {
    /**
     * 日志对象
     */
    protected Logger logger = (Logger) LoggerFactory.getLogger(getClass());
    @Autowired
    protected M dao;
    private List<baseMenu> list;
    //获取菜单
    public JSONArray getMenu(Integer[] rid) {
        list = dao.getRoleMenus(rid);
        JSONArray array = new JSONArray();
        JSONArray array2 = new JSONArray();
        for(baseMenu menu : list){
            if("0".equals(menu.getFid())){
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu);
                jsonObject.put("children",build(menu.getId(),rid));
                array.add(jsonObject);
            }
        }
        return array;
    }


    private JSONArray build(String id, Integer[] rid){
        //leave+=1;
        JSONArray buildarray = new JSONArray();
        for (baseMenu menu2 : list) {
            if(menu2.getFid().equals(id)){
                /*menu2.setLeava(leave);*/
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu2);
                jsonObject.put("children",build(menu2.getId(),rid));
                buildarray.add(jsonObject);
                //lists.add(menu2);
            }
        }
        return buildarray;

    }

    /**
     * 获取所有菜单（不需要权限）
     * @param
     * @return
     */
    public JSONArray getMenus() {
        list= dao.getMenus();
        JSONArray array = new JSONArray();
        JSONArray array2 = new JSONArray();
        for(baseMenu menu : list){
            if("0".equals(menu.getFid())){
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu);
                jsonObject.put("children",builds(menu.getId()));
                array.add(jsonObject);
            }
        }
        return array;
    }
    public JSONArray builds(String id){
        //leave+=1;
        JSONArray buildarray = new JSONArray();
        for (baseMenu menu2 : list) {
            if(menu2.getFid().equals(id)){
                /*menu2.setLeava(leave);*/
                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(menu2);
                JSONArray arr=builds(menu2.getId());//获取制定父节点的儿子
                List<baseFunction>flist=dao.functionList(menu2.getId());//获取菜单下的所有功能
                for(baseFunction bf:flist){
                    arr.add(bf);
                }
                jsonObject.put("children",arr);

                buildarray.add(jsonObject);
                //lists.add(menu2);
            }
        }
        return buildarray;
    }

    public JSONObject getFunctions(String menuId){
        JSONObject jsonObject=new JSONObject();
        List<baseFunction> baseFunctions=new ArrayList<>();
        List<baseFunction> flist=dao.functionList(menuId);//获取菜单下的所有功能
        try{
            UserInfo userInfo = UserInfoUtils.getUserInfo();
            baseFunctions.addAll(flist);//userInfo.getFunctions());
            //用户中心2.0 暂时无法使用   注释掉  --2018/08/10    用户中心可用在加上
/*            for(Function function : userInfo.getFunctions()){
                for(baseFunction baseFunctions1:flist){
                    if(baseFunctions1.getCode().equals(function.getfCode())){
                        baseFunctions.add(baseFunctions1);
                    }
                }
            }*/
        }catch (Exception ex){
            ex.printStackTrace();
        }
        //表格中行的按钮
        List<baseFunction> tableBtn=new ArrayList<>();
        //头部按钮
        List<baseFunction> headBtn=new ArrayList<>();
        for(baseFunction functions : baseFunctions){
            if(!"1".equals(functions.getStyle())){
                tableBtn.add(functions);
            }
            if(!"2".equals(functions.getStyle())){
                headBtn.add(functions);
            }
        }
        jsonObject.put("tableBtn",tableBtn);
        jsonObject.put("headBtn",headBtn);
        return jsonObject;

    }

}
